// pages/redpacket/my.js
import Router from '../../utils/router.js';
import base from '../../config/base.js';
import util from '../../utils/util.js'
import redpacket from '../../services/redpacket.js'
import shareInfoJson from '../../utils/hbshare.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    redpacket_i: base.static + '/redenvelopes/redpacket_i.png',
    noredpacket: base.static + '/redenvelopes/noredpacket.png',
    list: [],
    listlength: 1,
    //加载状态  1 还有数据可加载，0已全部加载完成  -1加载失败
    loadflag: 1,
    //加载 截流 true可加载
    listflag: false,
    offset: 1,
    limit: 20,
    sun_money_flag: true,
    sun_money: "0.00"
  },
  gostore(){
    Router.go('store');
  },
  goGonglue() {
    shareInfoJson.cardInfo.goGonglue();
  },
  getDetails() {
    let loadflag = 1,
      offset = this.data.offset,
      limit = this.data.limit,
      sun_money = this.data.sun_money,
      datas = {
        offset,
        limit
      };
    redpacket.getBountyBonusDetails(datas, res => {
      if (res && res.code == 1) {
        var list = res.data.list;
        if (this.data.sun_money_flag) {
          this.setData({
            sun_money: res.data.sum_money,
            sun_money_flag: false
          });
        }
        for (let index in list) {
          if (list[index].bybslt_money != null && list[index].bybslt_money != '' && !isNaN(Number(list[index].bybslt_money))) {
            list[index].bybslt_money = util.toDecimal2(util.floatAccDiv(list[index].bybslt_money, 1));
          }
        }
        list = this.data.list.concat(list);
        if (res.data.count <= list.length) {
          loadflag = 0;
        } else {
          offset++;
        }
        this.setData({
          list,
          listlength: list.length,
          loadflag,
          offset
        });
      } else {
        list = this.data.list;
        loadflag = 0;
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
        this.setData({
          loadflag,
          listlength: list.length,
          list: list
        });
      }
    }, res => {
      this.setData({
        listflag: true
      });
      wx.stopPullDownRefresh();
      if (res && res.statusCode != 200) {
        this.setData({
          loadflag: -1
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDetails();
    wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      listlength: [],
      loadflag: 1,
      offset: 1,
    });
    this.getDetails();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let loadflag = this.data.loadflag,
      listflag = this.data.listflag;
    if (loadflag == 1) {
      if (!listflag) {
        return;
      }
      this.setData({
        listflag: false
      });
      this.getDetails();
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // }
})